package com.example.demo.hcontroller;

import java.io.IOException;
import java.io.Writer;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.example.demo.pojo.Houser;
import com.example.demo.pojo.User;
import com.example.demo.service.IHouserService;
import com.example.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;

@Controller
@RequestMapping("/hlogin")
public class LoginController {

    @Resource
    private IUserService userService;

    @Autowired
    private IHouserService HouserService;
    @RequestMapping("/logout")
    public String logout(HttpSession session, Model model) {
        session.removeAttribute("user");
        return "login";
    }

    @RequestMapping("/viewlogin")
    public String viewpaper(HttpServletRequest request, Model model) {

        return "login";
    }

    //
    @RequestMapping("/checklogin")
    public void checklogin(HttpSession session, HttpServletResponse response, Houser Houser, Integer role, User user, String tel, String pass)
            throws IOException {

        System.out.println("name:"+tel+" pass:"+pass+" role:"+role);
        user=userService.selectCheckLogin(tel, pass);
        Houser=HouserService.HouserLogin(tel, pass);
        String s="";
        if(role==0){
            if(Houser==null) {
                s="0";
                Gson gson = new Gson();
                String json = gson.toJson(s);
                response.setHeader("Access-Control-Allow-Origin", "*");
                response.setHeader("Access-Control-Allow-Methods", "GET,POST");
                response.setContentType("text/json,charset=utf-8");
                response.setCharacterEncoding("utf-8");
                Writer out = response.getWriter();
                out.write(json);
                out.flush();
            }else {
                s="1";
                session.setAttribute("user_name", Houser.getLogo());
                session.setAttribute("sqxl", Houser);
                session.setAttribute("role", role);
                Gson gson = new Gson();
                String json = gson.toJson(s);
                response.setHeader("Access-Control-Allow-Origin", "*");
                response.setHeader("Access-Control-Allow-Methods", "GET,POST");
                response.setContentType("text/json,charset=utf-8");
                response.setCharacterEncoding("utf-8");
                Writer out = response.getWriter();
                out.write(json);
                out.flush();
            }
        }else{
            if(user==null) {
                s="0";
                Gson gson = new Gson();
                String json = gson.toJson(s);
                response.setHeader("Access-Control-Allow-Origin", "*");
                response.setHeader("Access-Control-Allow-Methods", "GET,POST");
                response.setContentType("text/json,charset=utf-8");
                response.setCharacterEncoding("utf-8");
                Writer out = response.getWriter();
                out.write(json);
                out.flush();
            }else {
                s="1";
                session.setAttribute("user_name", user.getUser_name());
                session.setAttribute("sqxl", user);
                session.setAttribute("role", role);
                Gson gson = new Gson();
                String json = gson.toJson(s);
                response.setHeader("Access-Control-Allow-Origin", "*");
                response.setHeader("Access-Control-Allow-Methods", "GET,POST");
                response.setContentType("text/json,charset=utf-8");
                response.setCharacterEncoding("utf-8");
                Writer out = response.getWriter();
                out.write(json);
                out.flush();
            }
        }



    }
}
